/**
 * 
 */
package com.nikolvas.normdb;

import java.util.List;

import com.nikolvas.interpolation.IFunction;
import com.nikolvas.interpolation.LinearInterpolator;
import com.nikolvas.interpolation.NumUtil;
import com.nikolvas.interpolation.Pair;
import com.nikolvas.man.Age;
import com.nikolvas.man.Sex;

/**
 * @author vasiliy
 *
 */
public class NormData {

	static final float GAP = -10000f;
	
	private final Sex sex;
	private final Age age;
	private final List<List<Float>> data;
	private final List<String> headers;
	
	public NormData(Sex sex, Age age, List<String> headers, List<List<Float>> data) {
		super();
		this.sex = sex;
		this.age = age;
		this.data = data;
		this.headers = headers;
	}
	
	public Sex getSex() {
		return sex;
	}
	
	public Age getAge() {
		return age;
	}
	
	public List<List<Float>> getData() {
		return data;
	}

	
	public List<String> getHeaders() {
		return headers;
	}

	public int getResult(int normNum, float val){
		List<Float> row = data.get(normNum);
		
		Pair p1 = new Pair(row.get(0),0);
		Pair p2;	
		for(int i = 1; i < row.size(); i++){
			p2 = new Pair(row.get(i),i);	
			if( GAP == p2.getX() ){
				continue;
			}
			if(NumUtil.isBetween(val, p1.getX(), p2.getX())){								
				IFunction f =  LinearInterpolator.interpolate(p1, p2);
				return Math.round( f.getY(val) );
			}
			p1 = p2;
		}
		throw new RuntimeException("No result for val = "+val);
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((age == null) ? 0 : age.hashCode());
		result = prime * result + ((sex == null) ? 0 : sex.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		NormData other = (NormData) obj;
		if (age == null) {
			if (other.age != null)
				return false;
		} else if (!age.equals(other.age))
			return false;
		if (sex == null) {
			if (other.sex != null)
				return false;
		} else if (!sex.equals(other.sex))
			return false;
		return true;
	}	
		
}
